package com.handpay.zztong.hp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.handpay.framework.swiper.SwiperMode;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.Address;
import com.handpay.zztong.hp.bean.Rate;
import com.handpay.zztong.hp.bean.Rates;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.trans.activity.VPOSActivity;

import java.util.ArrayList;
import java.util.Hashtable;

public class Muiltate extends ZZTongActivity implements View.OnClickListener {
    private Muiltate mContext;
    //	private ImageView left_bar;
    private Button confirm;
    //	private TextView title;
    private Double amount;
    private Rates mRates = null;
    private ArrayList<Rate> lists = new ArrayList<Rate>();
    private int isPubilcChargeId;
    private double rateCode;
    private Rate mRate = null;
    private ListView lvMulitRate;
    private ListMulitRateAdapter listMulitRateAdapter;
    private int cur_pos = -1;
    private int isOpen = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.hp_mulitrate);
        super.onCreate(savedInstanceState);
        mContext = this;
        confirm = (Button) findViewById(R.id.btn_confirmRate);
        confirm.setOnClickListener(this);
        mRates = (Rates) getIntent().getSerializableExtra("rates");
        amount = getIntent().getDoubleExtra("amount", 0d);
        HPLog.i("Muiltate", "amount:" + amount);
        lists = mRates.getRates();
        mRate = new Rate();
        isOpen = getIntent().getIntExtra("isOpen", 0);
        lvMulitRate = (ListView) findViewById(R.id.lvMulitRate);
        listMulitRateAdapter = new ListMulitRateAdapter(this, R.layout.mulitrate_item);
        lvMulitRate.setAdapter(listMulitRateAdapter);
        lvMulitRate.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lvMulitRate.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                mRate = lists.get(position);
                rateCode = mRate.getRateCodeId();
                isPubilcChargeId = mRate.getIsPublicChargeId();
                HPLog.i("Muiltate", "rateCode:" + rateCode);
                cur_pos = position;
                listMulitRateAdapter.notifyDataSetChanged();
            }
        });
        for (int i = 0; i < lists.size(); i++) {
            if (lists.get(i).isSelectDefault()) {
                lvMulitRate.setSelection(i);
                cur_pos = i;
                listMulitRateAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected boolean isShownTopBar() {
        return true;
    }

    @Override
    public ActionBar getActivityActionBar() {

        return new ActionBar(this.getString(R.string.choose_paymentMethods), true);
    }

    @Override
    protected boolean isShownTabs() {
        return false;
    }

    @Override
    public void goToVPos() {
        super.goToVPos();
        getSelectedRate();
    }

    private void getSelectedRate() {
        setMode(SwiperMode.NORMAL_TRANS, false, new Runnable() {
            @Override
            public void run() {
                if (Address.IS_CHECK_ADDRESS && !showNoTranctionMsg()) {
                    HPLog.i(HPLog.FDL_TAG, "不在交易范围之内");
                    return;
                }
                Hashtable<String, Object> params = new Hashtable<String, Object>();
                params.put(ZZTongActivity.ACTION, ZZTongActivity.Action_Transfer1);
                params.put(VPOSActivity.Transfer_Amount_Key, amount * 100);
                params.put("selectedRate", rateCode);
                params.put("isPublicCharge", isPubilcChargeId);
                params.put("isRateNull", mRates.isRateNull());
                startActivity(Muiltate.this, VPOSActivity.class, params);
                goBack();
            }
        });
    }

    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_confirmRate) {
            getSelectedRate();
        }
    }

    class ListMulitRateAdapter extends BaseAdapter {
        private int mItemLayoutId;
        private LayoutInflater mInflater;

        public ListMulitRateAdapter(Context context, int resourceId) {
            mInflater = LayoutInflater.from(context);
            mItemLayoutId = resourceId;
        }

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("NewApi")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
//			ArrayList<Rate> mLists = new ArrayList<Rate>();
            Rate mRate = new Rate();
            mRate = lists.get(position);
            if (null == convertView) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(mItemLayoutId, null);
                holder.rateText = (TextView) convertView.findViewById(R.id.rate_tv);
                holder.select_rate = (ImageView) convertView.findViewById(R.id.select_rate);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.rateText.setText(lists.get(position).getRateName());
            if (cur_pos == position) {
                holder.select_rate.setBackground(getResources().getDrawable(R.drawable.success_select));
            } else {
                holder.select_rate.setBackground(getResources().getDrawable(R.drawable.success_hover));
            }
            return convertView;
        }

        private class ViewHolder {
            TextView rateText;
            ImageView select_rate;
        }
    }

//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
//		//path：/storage/emulated/0/zzTong/cardImg/1448526156698.jpg
//
//		if(requestCode == ZZTConstant.REQUEST_CAMERA_CODE){
//			String fileUrl = null;
//			if(resultCode == Activity.RESULT_OK){
//				//保存成功
//
//				if(data != null && data.getStringExtra("FileUrl") != null){
//					fileUrl = data.getStringExtra("FileUrl");
//					HPLog.e(HPLog.FDL_TAG,"FileUrl"+data.getStringExtra("FileUrl"));
//				}
//				HPLog.e(HPLog.FDL_TAG, "onActivityResult--保存成功 ");
//			}else{
//				//保存失败
//				HPLog.e(HPLog.FDL_TAG, "onActivityResult--保存失败");
//			}
//			goTo_Vpos_Activity(null,fileUrl);
//		}
//	}

}
